"""

"""

from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.app import App
from ..components.progress_display import ProgressDisplay

class ProcessingWindow(Screen):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._build_ui()
    
    def _build_ui(self):
        """"""
        main_layout = BoxLayout(
            orientation='vertical',
            spacing=20,
            padding=20
        )
        
        # 
        title = Label(
            text="Processing Video",
            font_size=48,  # 放大两倍字体
            size_hint_y=None,
            height=80,
            bold=True,
            color=(0.2, 0.6, 0.9, 1)
        )
        main_layout.add_widget(title)
        
        # 
        self.progress_display = ProgressDisplay()
        main_layout.add_widget(self.progress_display)
        
        # 
        button_layout = BoxLayout(
            orientation='horizontal',
            size_hint_y=None,
            height=50,
            spacing=10
        )
        
        # 
        self.cancel_btn = Button(
            text="Cancel Processing",
            size_hint_x=0.5,
            height=70,
            font_size=24  # 放大字体
        )
        self.cancel_btn.bind(on_press=self._on_cancel)
        button_layout.add_widget(self.cancel_btn)
        
        # 
        self.result_btn = Button(
            text="View Results",
            size_hint=(0.4, None),
            height=70,
            font_size=24,  # 放大字体
            background_color=(0.2, 0.6, 0.9, 1),
            color=(1, 1, 1, 1),
            disabled=True
        )
        self.result_btn.bind(on_press=self._on_view_result)
        button_layout.add_widget(self.result_btn)
        
        main_layout.add_widget(button_layout)
        
        self.add_widget(main_layout)
    
    def start_processing(self, file_path):
        """"""
        # TODO: 
        self.progress_display.update_progress(
            0, 
            "Starting processing...", 
            f"File: {file_path.split('/')[-1]}"
        )
        
        # 
        self._simulate_processing()
    
    def _simulate_processing(self):
        """"""
        # TODO: 
        import threading
        import time
        
        def process():
            steps = [
                (10, "Extracting Audio...", "Extracting audio track from video"),
                (30, "Loading Model...", "Loading Whisper transcription model"),
                (50, "Transcribing...", "Performing speech recognition"),
                (70, "Connecting AI...", "Connecting to Ollama service"),
                (90, "Generating Summary...", "Using AI to generate intelligent summary"),
                (100, "Processing Complete!", "Video summary generation completed")
            ]
            
            for progress, status, detail in steps:
                self.progress_display.update_progress(progress, status, detail)
                time.sleep(2)
            
            # 
            self.result_btn.disabled = False
            self.cancel_btn.text = " Process New Video"
        
        thread = threading.Thread(target=process)
        thread.daemon = True
        thread.start()
    
    def _on_cancel(self, instance):
        """"""
        if self.cancel_btn.text == " Process New Video":
            app = App.get_running_app()
            app.navigate_to('upload')
        else:
            # TODO: 
            app = App.get_running_app()
            app.navigate_to('main')
    
    def _on_view_result(self, instance):
        """"""
        app = App.get_running_app()
        app.navigate_to('result')